package smartcode;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.ThemedSpinnerAdapter;

import com.aliang.auto.R;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import app.auto.runner.base.SPrefUtil;
import app.auto.runner.base.intf.FunCallback;
import app.auto.runner.base.utility.ShareAlert;
import app.auto.runner.base.utility.ToastUtil;
import app.auto.runner.base.utility.VerifyCode;
import app.auto.runner.base.widget.expandable.ExpandableAdapter;
import smartcode.privacy.BaseActivity;
import smartcode.privacy.PrivacyHelper;

public class ClassActivity extends BaseActivity {

    private ExpandableListView expand_list_id;
    private List<String> groups = new ArrayList<>(Arrays.asList("字段", "方法"));
    private List<List> childs = new ArrayList<>();

    public List<String> fields = new ArrayList<>();
    public List<String> methods = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }

    @Override
    public int intiLayout() {
        return R.layout.activity_class;
    }

    @Override
    public void initView() {
        SPrefUtil.iniContext(this);
        String first = (String) SPrefUtil.getValue("first");
        if (first.equals("false") || first.equals("")) {
            PrivacyHelper.get(PrivacyHelper.class, this).showDescOppo(null);
        }
        ((TextView) findViewById(R.id.title)).setText("NewClass");
        childs.add(fields);
        childs.add(methods);
        fields.add("f1");
        fields.add("f2");
        methods.add("public static void material(){...}");
        methods.add("public static void classify(){...}");

        initView2();
    }

    ExpandableListviewAdapter adapter;

    int optgroup;
    int optchild;

    void initView2() {
        findViewById(R.id.bbar).findViewById(R.id.newone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (optgroup == 0) {
                    fields.add(optchild, VerifyCode.getVerifycode3());
                } else {
                    methods.add(optchild, VerifyCode.getVerifycode3());
                }
                adapter.notifyDataSetChanged();
                findViewById(R.id.bbar).findViewById(R.id.edit).performClick();
            }
        });
        findViewById(R.id.bbar).findViewById(R.id.del).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int groupPosition = optgroup;
                int childPosition = optchild;
                childs.get(groupPosition).remove(childPosition);
                adapter.notifyDataSetChanged();
            }
        });
        findViewById(R.id.bbar).findViewById(R.id.edit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int groupPosition = optgroup;
                int childPosition = optchild;
                startActivity(new Intent(v.getContext(), CodeActivity.class).putExtra("name", childs.get(groupPosition).get(childPosition).toString()));
            }
        });
        expand_list_id = findViewById(R.id.expand_listview);
        adapter = new ExpandableListviewAdapter(this, groups, childs);
        expand_list_id.setAdapter(adapter);
        //默认展开第一个数组
        expand_list_id.expandGroup(0);
        //关闭数组某个数组，可以通过该属性来实现全部展开和只展开一个列表功能
        expand_list_id.expandGroup(1);
        expand_list_id.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int groupPosition, long l) {
                showToastShort(groups.get(groupPosition));
                return false;
            }
        });

        //子视图的点击事件
        expand_list_id.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
                findViewById(R.id.bbar).setVisibility(View.VISIBLE);
                optgroup = groupPosition;
                optchild = childPosition;
                adapter.groupPosition = groupPosition;

                adapter.childPosition = childPosition;
                adapter.notifyDataSetChanged();

                startActivity(new Intent(view.getContext(), CodeActivity.class).putExtra("name", childs.get(groupPosition).get(childPosition).toString()));

                findViewById(R.id.bbar).findViewById(R.id.edit).performClick();

                return true;
            }
        });
        //用于当组项折叠时的通知。
        expand_list_id.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {
                showToastShort("折叠了数据___" + groups.get(groupPosition));
            }
        });
        //
        //用于当组项折叠时的通知。
        expand_list_id.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {
                showToastShort("展开了数据___" + groups.get(groupPosition));
            }
        });


    }

    private void showToastShort(String s) {
//        ToastUtil.live(this).toast(s);
    }

    @Override
    public void onBackPressed() {
        if (findViewById(R.id.bbar).getVisibility() == View.VISIBLE) {
            findViewById(R.id.bbar).setVisibility(View.GONE);
            adapter.childPosition = -1;
            adapter.groupPosition = -1;
            adapter.notifyDataSetChanged();
            return;
        }
        super.onBackPressed();

    }
}

//对象 和 内存 欢迎
// 解析
// new Storage().getFile();

//构造函数
//复合
//()